#
# Author :: Madhu CK <madhu_c_k@us.ibm.com>
# Cookbook Name:: was
# Recipe:: sce_init
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

# Most of this recipe is sce specific; expect was activation engine

require 'rexml/document'
xml_data = File.read('/etc/cloud/parameters.was.done')
params = REXML::Document.new(xml_data)
wasuser = params.root.elements["field[@name='WASAdminUser']/values/value"].text
wasuserpw = params.root.elements["field[@name='WASAdminPassword']/values/value"].text
node.default.set[:was][:user] = wasuser
node.default.set[:was][:userpw] = wasuserpw

# Should use a service here, but do not know if we can pass in a environment var
bash "stop_default_appserver" do
  code <<-EOH
  echo "Stopping WAS with user #{wasuser}"
  #{node[:was][:installDir]}/../Profiles/AppSrv01/bin/stopServer.sh server1 -username #{wasuser} -password #{wasuserpw} 
  EOH
  not_if "/etc/init.d/AppSrv01-server1_was.init status | grep not"
end

bash "delete_default_profile" do
  user wasuser
  cwd "#{node[:was][:installDir]}"
  code <<-EOH
  echo "Deleting default AppSrv01 profile"
  bin/manageprofiles.sh -delete -profileName AppSrv01
  EOH
  not_if "test -d #{node[:was][:installDir]}/../Profiles/AppSrv01"
end
